package com.heima.article;

import com.heima.article.service.ApArticleService;
import com.heima.common.constants.message.WmNewsUpDownConstants;
import com.heima.common.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

import static com.heima.common.constants.message.WmNewsUpDownConstants.WM_NEWS_UP_OR_DOWN_TOPIC;

@Slf4j
@Component
public class WmNewsUpOrDownListener {

    @Autowired
    private ApArticleService apArticleService;

    @KafkaListener(topics = WM_NEWS_UP_OR_DOWN_TOPIC)
    public void receiveMsg(ConsumerRecord<String,String> record){
        Optional<ConsumerRecord<String, String>> optional = Optional.ofNullable(record);
        if(optional.isPresent()){
            String value = record.value();
            log.info("接收到消息={}",value);
            Map<String, Long> map = JsonUtils.toMap(value, String.class, Long.class);
//            修改article的上下架
            apArticleService.upOrDown(map);
        }

    }
}
